package cn.tedu.online.examination.system.ui;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

/**
 * 二、登陆页面
 * 1、加载登陆窗口
 * 2、加入文本输入框、密码框
 * 3、验证码
 * 4、加入登陆、取消按钮
 * 4.1登陆模拟实现
 * 4.2取消弹出按钮
 *
 * @author JokerQ
 * @date 2020/6/8 - 17:26
 */
public class LoginFrame extends JFrame implements Serializable {

    private static final long serialVersionUID = 6951504976525479120L;
    /**
     * 验证码
     */
    private JTextField chacking;
    private JLabel chack;
    /**
     * id
     */
    private JTextField idField;
    /**
     * 密码
     */
    private JPasswordField pwd;

    private JLabel message;

    private ClientContext clientContext;

    public LoginFrame() {
        init();
    }

    public void init() {
        this.add(createContentPane());
        this.setTitle("在线考试");
        this.setSize(800, 600);
        //设置此框架是否可由用户调整大小
        this.setResizable(false);
        /* 创建面板，这个类似于 HTML 的 div 标签
         * 我们可以创建多个面板并在 this 中指定位置
         * 面板中我们可以添加文本字段，按钮及其他组件。
         */
        /**
         * public GridLayout(int rows,
         *                   int cols,
         *                   int hgap,
         *                   int vgap)
         * GridLayout类是一个布局管理器，它将一个容器的组件放在矩形网格中。
         * 容器被分成等大小的矩形，并且每个矩形中放置一个组件
         *
         * rows - 行，值为零表示任意数量的行
         * cols - 值为零，表示任意数量的列
         * hgap - 水平间隙
         * vgap - 垂直间隙
         * 水平和垂直间隙被设置为指定的值。
         * 每个列之间放置水平间隙。 在每行之间放置垂直间隙。
         */
       /* jPanel.add(BorderLayout.WEST,new JLabel("用户名"));
        jPanel.add(username);
        jPanel.add(BorderLayout.SOUTH,new JLabel("密码"));
        jPanel.add(password);
        jPanel.add(new JLabel("验证码"));
        jPanel.add(veriCode);*/
        // 添加面板
        //this.add(jPanel);
        // 设置界面可见
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
    }

    /**
     * 地基面板
     * 在此面板上，分为三个区块
     * 1、JLbael：登陆系统
     * 2、JTextField/JPasswordField/JLbael：id+password+vericode
     *      2.1 id+password一个区域，直接填充满横向全部
     *      2.2 vericode：JTextField（填写验证码）+JLbael（默认验证码+看不清）
     * 3、JButton：Login/Cancel
     *
     * @return
     */
    private JPanel createContentPane() {
        JPanel pane = new JPanel(new BorderLayout());
        pane.setBorder(new EmptyBorder(8, 8, 8, 8));
        pane.setBackground(Color.YELLOW);
        pane.add(BorderLayout.NORTH, new JLabel("登录系统", JLabel.CENTER));
        pane.add(BorderLayout.CENTER, createCenterPane());
        pane.add(BorderLayout.SOUTH, createButtonPane());
        return pane;
    }

    /**
     * 核心的面板
     * 用户id和密码填写部分
     * 分区显示好处：不全部填充
     * @return
     */
    private JPanel createCenterPane() {
        JPanel pane = new JPanel(new BorderLayout());
        pane.setBorder(new EmptyBorder(50, 10, 10, 10));
        pane.add(BorderLayout.NORTH, createIdPwdPane());
        pane.setBackground(Color.GREEN);
        return pane;
    }

    public void setClientContext(ClientContext clientContext) {
        this.clientContext = clientContext;
    }

    /**
     * 按钮部分
     * Login&Cancel
     * @return
     */
    private JPanel createButtonPane() {

        JPanel pane = new JPanel(new FlowLayout());
        JButton login = new JButton("Login");
        JButton register = new JButton("Register");
        JButton cancel = new JButton("Cancel");
        pane.setBackground(Color.RED);
        pane.add(login);
        pane.add(register);
        pane.add(cancel);

        getRootPane().setDefaultButton(login);
        /**
         * 添加单击按钮监听事件
         * （注入clientContext并添加set方法）
         */
        login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("login Botton Click!");
                System.out.println(clientContext);
                clientContext.login();
            }

        });
        cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clientContext.exit(LoginFrame.this);
            }

        });
        register.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clientContext.register();
            }
        });
        return pane;
    }

    private JPanel createIdPwdPane() {
        JPanel pane = new JPanel(new GridLayout(3, 1, 0, 6));
        pane.setBackground(Color.ORANGE);
        /**
         * 面板中添加id
         */
        pane.add(createIdPane());
        /**
         * 面板中添加password
         */
        pane.add(createPwdPane());
        /**
         * 面板中加入验证码
         */
        pane.add(createCheckPane());
        return pane;
    }

    private JPanel createIdPane() {
        JPanel pane = new JPanel(new BorderLayout());
        pane.add(BorderLayout.WEST, new JLabel("编号：   "));
        idField = new JTextField();
        pane.add(BorderLayout.CENTER, idField);
        return pane;
    }

    private JPanel createPwdPane() {
        JPanel pane = new JPanel(new BorderLayout());
        pane.add(BorderLayout.WEST, new JLabel("密码：   "));
        pwd = new JPasswordField();
        pwd.enableInputMethods(true);
        pane.add(BorderLayout.CENTER, pwd);
        return pane;

    }

    private JPanel createCheckPane(){
        JPanel pane = new JPanel(new BorderLayout());
        pane.add(BorderLayout.CENTER, createCheckingPane());
        pane.add(BorderLayout.EAST, createRandomCheckPane());
        return pane;
    }

    private JPanel createRandomCheckPane() {
        JPanel pane = new JPanel(new BorderLayout());
        chack = new JLabel("1234");
        pane.setBorder(new EmptyBorder(0, 10, 0, 10));
        JLabel randomCode = new JLabel("   看不清换一组");
        randomCode.setBackground(Color.BLUE);
        pane.add(BorderLayout.CENTER, chack);
        pane.add(BorderLayout.EAST, randomCode);
        randomCode.setBackground(null);
        return pane;
    }

    private JPanel createCheckingPane() {
        JPanel pane = new JPanel(new BorderLayout());
        chacking = new JTextField(1);
        pane.add(BorderLayout.WEST, new JLabel("验证码："));
        pane.add(BorderLayout.CENTER, chacking);
        return pane;
    }


    /**
     * 验证码获取
     */
    public String getChack(){
        System.out.println(chack.getText().toUpperCase());
        return chack.getText().toUpperCase();
    }
    /**
     * 用户填写验证码获取
     */
    public String getChacking(){
        System.out.println(chacking.getText().toUpperCase());
        return chacking.getText().toUpperCase();
    }

    /**
     * 通过登陆界面组件获取填写信息
     */
    public int getUserId() {
        return Integer.parseInt(idField.getText());
    }

    public String getPwd() {
        return new String(pwd.getPassword());
    }

}
